package com.palm.core.ui.tags;

import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import org.noear.solon.Solon;
import org.noear.solon.core.handle.*;
import org.noear.solon.core.util.PrintUtil;

public class DoTag extends Directive {
    @Override
    public void exec(Env env, Scope scope, Writer writer) {
        //获取调用路径
        String methodPath=(String) exprList.getFirstExpr().eval(scope);
        if(methodPath==null){
            return;
        }
        if(!methodPath.startsWith("/")){
            methodPath="/"+methodPath;
        }
        Context ctx=Context.current();
        if(ctx==null){
            PrintUtil.warn("Context is null");
            return;
        }
        ctx.pathNew(methodPath.replaceAll("\\.","/"));
        Handler handler= Solon.global().router().matchOne(ctx, Endpoint.main);
        ctx.pathNew(null);
        if(handler!=null&&handler instanceof Action){
            Action ac=(Action) handler;
            Object ret= new TagInnerAction(ac).invoke(ctx);
            if(ret!=null){
                if(ret instanceof Result){
                    ret=((Result<?>) ret).getData();
                }
                scope.set("data",ret);
            }
           // System.out.println(((Ret)ret).getMessage());
        }else{
            PrintUtil.error("page: "+ctx.path()+" ,api method ["+methodPath+"] not found!");
        }
    }
}
